/* @INLINE FILE util.bloc.js =============================================================
	* Utility functions
	* 
	* VAR
	* mseed - random generator for get_random
	* escapable
	* meta
	* 
	* FUNC
	* escapeStr - escape text
	* _unescape - unescape text
	* checkLength - Check the byte length of a utf-8 string
    * div - integer division
	* zpad_left - adds zeros on the left
	* zpad_right - adds zeros on the right
	* url_domain - Get the domain of a url string.
	* EN - Extract the numerical value of
	* get_random - generates a random number from mseed
	* $ - Get element from id in current document
	* $2 - Get element from id in IFrameDoc
    * centerBox
    * centerScreen
    * urlify
    * escapeJSONString
    * this.returnElementCoordinates
	*/
	
    var escapeStr = function(msg){
    	return msg.replace(/%/g, '%25').replace(/&/g, '%26amp;').replace(/</g, '%26lt;').replace(/>/g, '%26gt;').replace(/\\/g, '\\\\');
    };
    
    var _unescape = function(str){
    	return str.replace(/%26/g, '&').replace(/%25/g, '%')
    };
    
    var checkLength=function(countMe) {
        var escapedStr = encodeURI(countMe);
        if (escapedStr.indexOf("%") != -1) {
            var count = escapedStr.split("%").length - 1;
            if (count == 0){ 
            	count++;
            }
            var tmp = escapedStr.length - (count * 3);
            count = count + tmp;
        } else {
            count = escapedStr.length;
        }
        return count;
    };
    
    var div = function(op1, op2) {
  	  return(Math.round(op1 / op2 - op1 % op2 / op2));
  	};
  	
    var zpad_left = function(n,z){
    	if(!z){
    		z=5;
    	}
    	return String("0000000000" + n).slice(-z);
    };
    
    var zpad_right = function(n,z){
    	if(!z){
    		z=8;
    	}
    	return String(n+"0000000000").slice(0,z);
    };
    
    var url_domain = function(str) {
    	var a = document.createElement('a'); 
    	a.href = str;
        return a.hostname;
    };

    var EN=function(value) {
        var n = parseInt(value,10);
        return(n == null || isNaN(n) ? 0: n);
    };
    
    var mseed = 18000 * (13 & 65535) + (13 >> 16);
    var get_random=function(n)
    {
        var hex='';
        n = 36969 * (n & 65535) + (n >> 16);
        n = Math.abs((n << 16) + mseed);
        var s = '000000' + n;
        for(var i=s.length-6;i<s.length;i++){
        	hex+=hexArr[s.substr(i,1)];
        }
        return hex;
    };
    
    function $(id) {
        return document.getElementById(id);
    };
    
    function $2(id) {
        return IFrameDoc.getElementById(id);
    };
    
    //centerScreen e2 within e1
    var centerBox = function(e1,e2){
    	var coords = {};
    	coords.y = (e1.height()-e2.height())/2;
    	coords.x=(e1.width()-e2.width())/2;
		return coords;
    };
    
    //center element elem on screen
    var centerScreen = function(elem){
    	var coords = {};
    	coords.y = (jGo.util.getInnerHeight()-elem.height())/2;
    	if(exp6){
			coords.y+=jQ(document).scrollTop();
		}
		coords.x=(jGo.util.getSWidth()-elem.width())/2;
		return coords;
    };
    
    /*
     * link detection
     */
    var urlify = function(text) {
        var urlRegex = /(([a-z]+:\/\/)?(([a-z0-9\-]+\.)+([a-z]{2}|aero|arpa|biz|com|coop|edu|gov|info|int|jobs|mil|museum|name|nato|net|org|pro|travel|local|internal))(:[0-9]{1,5})?(\/[a-z0-9_\-\.~]+)*(\/([a-z0-9_\-\.]*)(\?[a-z0-9+_\-\.%=&amp;]*)?)?(#[a-zA-Z0-9!$&'()*+.=-_~:@/?]*)?)(\s+|$)/gi;
        return text.replace(urlRegex, function(url) {
        	if (!(/[a-z]+:\/\//.test(url))){
        		url="http://" + url;
        	}
            var url2 = url.replace(/^https?:\/\//, '');
            var utb1 = "youtube.com/watch?v=";
            var utbid=url.toLowerCase().indexOf(utb1);
            if(utbid>0){
            	var vid=url.substr(utbid+20).split('%26amp;')[0];
            	var h=60;
            	return '%d style="display:inline;height: '+h+'px;"%g%i SRC="' + _r + 'trans.gif" height="'+h+'px" width="1px" style="height:'+h+'px;width:1px;background-color: transparent;"%g%~d%a href="http://www.youtube.com/watch?v='+vid+'" target=_blank%g%i height="'+h+'px" style="height:'+h+'px" src="http://img.youtube.com/vi/'+vid+'/default.jpg" alt="" %g%~a =i=('+__(418,'YouTube video')+')=i=';
            }
            return '%a target=_blank href="' + url + '"%g' + url2 + '%~a';
        })
        // or alternatively
        // return text.replace(urlRegex, '<a href="$1">$1</a>')
    };
    
    //Escaping json
    var escapeable = /["\\\x00-\x1f\x7f-\x9f]/g;
    var meta = {
            '\b': '\\b',
            '\t': '\\t',
            '\n': '\\n',
            '\f': '\\f',
            '\r': '\\r',
            '\\': '\\\\'
    };
    var escapeJSONString = function( string, s ) {
    	meta['"']=s;
        if ( string.match( escapeable ) ) {
                return string.replace( escapeable, function( a ) {
                        var c = meta[a];
                        if ( typeof c === 'string' ) {
                                return c;
                        }
                        c = a.charCodeAt();
                        return '\\u00' + Math.floor(c / 16).toString(16) + (c % 16).toString(16);
                });
        }
        return string;
    };
    
    this.returnElementCoordinates = function(el) {
        var coords = {};
        coords.x = coords.y = 0;
        //if (el.offsetParent) {
            //do {
                coords.x = jQ(el).offset().left;
                coords.y = jQ(el).offset().top - (jQ(el).css("position")=="fixed"?jQ(document).scrollTop():0);
            //}
            while (el = el.offsetParent);
       // }
        return coords;
    };
    
    //util.bloc.js =============================================================@END FILE*/